#!/bin/bash

# 版本管理脚本
set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 默认配置
DEFAULT_MAIN_BRANCH="main"
DEFAULT_DEVELOP_BRANCH="develop"
DEFAULT_FEATURE_PREFIX="feature/"
DEFAULT_RELEASE_PREFIX="release/"
DEFAULT_HOTFIX_PREFIX="hotfix/"
DEFAULT_TAG_PREFIX="v"
DEFAULT_AUTO_PUSH="true"
DEFAULT_DELETE_MERGED_BRANCHES="true"

# 加载配置文件
load_config() {
    local config_file=".version.config"

    # 设置默认值
    MAIN_BRANCH=$DEFAULT_MAIN_BRANCH
    DEVELOP_BRANCH=$DEFAULT_DEVELOP_BRANCH
    FEATURE_PREFIX=$DEFAULT_FEATURE_PREFIX
    RELEASE_PREFIX=$DEFAULT_RELEASE_PREFIX
    HOTFIX_PREFIX=$DEFAULT_HOTFIX_PREFIX
    TAG_PREFIX=$DEFAULT_TAG_PREFIX
    AUTO_PUSH=$DEFAULT_AUTO_PUSH
    DELETE_MERGED_BRANCHES=$DEFAULT_DELETE_MERGED_BRANCHES

    # 如果配置文件存在，则加载配置
    if [ -f "$config_file" ]; then
        echo -e "${BLUE}📋 加载配置文件: $config_file${NC}"
        source "$config_file"
    else
        echo -e "${YELLOW}⚠️  配置文件不存在，使用默认配置${NC}"
        echo -e "${BLUE}💡 可以创建 $config_file 文件来自定义配置${NC}"
    fi

    echo -e "${GREEN}🔧 当前配置:${NC}"
    echo "  主分支: $MAIN_BRANCH"
    echo "  开发分支: $DEVELOP_BRANCH"
    echo "  自动推送: $AUTO_PUSH"
    echo ""
}

# 显示帮助信息
show_help() {
    echo -e "${BLUE}用户管理系统 - 版本管理工具${NC}"
    echo ""
    echo "用法: $0 [COMMAND] [OPTIONS]"
    echo ""
    echo "命令:"
    echo "  init         初始化Git Flow"
    echo "  feature      创建/完成功能分支"
    echo "  release      创建/完成发布分支"
    echo "  hotfix       创建/完成热修复分支"
    echo "  tag          创建版本标签"
    echo "  status       查看当前状态"
    echo ""
    echo "示例:"
    echo "  $0 init                           # 初始化Git Flow"
    echo "  $0 feature start user-profile    # 开始功能开发"
    echo "  $0 feature finish user-profile   # 完成功能开发"
    echo "  $0 release start v1.1.0          # 开始发布准备"
    echo "  $0 release finish v1.1.0         # 完成发布"
    echo "  $0 hotfix start v1.0.1           # 开始热修复"
    echo "  $0 hotfix finish v1.0.1          # 完成热修复"
    echo "  $0 tag v1.1.0 'Release v1.1.0'   # 创建版本标签"
    echo ""
    echo -e "${YELLOW}注意: 首次使用请先运行 '$0 init' 初始化Git Flow${NC}"
}

# 检查Git Flow是否已初始化
check_gitflow_init() {
    # 检查是否存在开发分支
    if ! git show-ref --verify --quiet refs/heads/$DEVELOP_BRANCH; then
        echo -e "${RED}❌ 错误: Git Flow尚未初始化${NC}"
        echo ""
        echo -e "${YELLOW}请先运行以下命令初始化Git Flow:${NC}"
        echo -e "${GREEN}  $0 init${NC}"
        echo ""
        echo -e "${BLUE}Git Flow初始化将会:${NC}"
        echo "  • 确保${MAIN_BRANCH}分支存在"
        echo "  • 创建${DEVELOP_BRANCH}分支"
        echo "  • 设置分支关联关系"
        echo ""
        echo -e "${YELLOW}初始化后您就可以使用所有功能了！${NC}"
        exit 1
    fi
}

# 检查是否在Git仓库中
check_git_repo() {
    if ! git rev-parse --git-dir > /dev/null 2>&1; then
        echo -e "${RED}❌ 错误: 当前目录不是Git仓库${NC}"
        echo ""
        echo -e "${YELLOW}请在Git仓库根目录下运行此脚本${NC}"
        exit 1
    fi
}

# 验证版本号格式
validate_version_format() {
    local version=$1
    local version_type=$2  # release 或 hotfix

    # 移除可能的v前缀进行验证
    local clean_version=${version#v}

    # 检查基本的语义化版本格式 x.y.z
    if [[ ! "$clean_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
        echo -e "${RED}❌ 错误: 版本号格式不正确${NC}"
        echo ""
        echo -e "${YELLOW}版本号必须遵循语义化版本规范:${NC}"
        echo "  格式: [v]主版本.次版本.修订版本"
        echo "  示例: v1.2.0, 2.1.3, v0.1.0"
        echo ""
        echo -e "${BLUE}语义化版本说明:${NC}"
        echo "  主版本: 不兼容的API修改"
        echo "  次版本: 向下兼容的功能性新增"
        echo "  修订版本: 向下兼容的问题修正"
        exit 1
    fi

    # 解析版本号
    IFS='.' read -r major minor patch <<< "$clean_version"

    # 检查版本号递增规则
    if [ "$version_type" = "release" ]; then
        # 发布版本建议次版本或主版本递增
        if [ "$minor" = "0" ] && [ "$patch" = "0" ]; then
            echo -e "${GREEN}✅ 主版本发布: $version${NC}"
        elif [ "$patch" = "0" ]; then
            echo -e "${GREEN}✅ 次版本发布: $version${NC}"
        else
            echo -e "${YELLOW}⚠️  警告: 发布版本建议使用 x.y.0 格式${NC}"
            echo -e "${BLUE}💡 修订版本通常用于热修复${NC}"
        fi
    elif [ "$version_type" = "hotfix" ]; then
        # 热修复版本应该是修订版本递增
        if [ "$patch" != "0" ]; then
            echo -e "${GREEN}✅ 热修复版本: $version${NC}"
        else
            echo -e "${YELLOW}⚠️  警告: 热修复通常应该递增修订版本号${NC}"
            echo -e "${BLUE}💡 建议格式: x.y.z (z > 0)${NC}"
        fi
    fi

    # 检查版本是否已存在
    if git tag | grep -q "^${TAG_PREFIX}${clean_version}$"; then
        echo -e "${RED}❌ 错误: 版本标签 ${TAG_PREFIX}${clean_version} 已存在${NC}"
        echo ""
        echo -e "${YELLOW}现有标签:${NC}"
        git tag | grep "^${TAG_PREFIX}" | sort -V | tail -5
        exit 1
    fi

    echo -e "${GREEN}✅ 版本号格式验证通过${NC}"
}

# 检测并设置主分支
detect_main_branch() {
    local detected_branch=""

    # 检查远程分支
    if git ls-remote --heads origin | grep -q "refs/heads/main"; then
        detected_branch="main"
    elif git ls-remote --heads origin | grep -q "refs/heads/master"; then
        detected_branch="master"
    elif git show-ref --verify --quiet refs/heads/main; then
        detected_branch="main"
    elif git show-ref --verify --quiet refs/heads/master; then
        detected_branch="master"
    fi

    if [ -n "$detected_branch" ] && [ "$detected_branch" != "$MAIN_BRANCH" ]; then
        echo -e "${YELLOW}⚠️  检测到主分支为: $detected_branch，但配置为: $MAIN_BRANCH${NC}"
        read -p "是否更新配置文件中的主分支名称？(y/N): " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            # 更新配置文件
            if [ -f ".version.config" ]; then
                sed -i "s/MAIN_BRANCH=.*/MAIN_BRANCH=$detected_branch/" .version.config
            else
                echo "MAIN_BRANCH=$detected_branch" > .version.config
            fi
            MAIN_BRANCH=$detected_branch
            echo -e "${GREEN}✅ 已更新主分支配置为: $MAIN_BRANCH${NC}"
        fi
    fi
}

# 初始化Git Flow
init_gitflow() {
    echo -e "${GREEN}🚀 初始化Git Flow...${NC}"

    # 检测主分支
    detect_main_branch

    # 确保在主分支
    git checkout $MAIN_BRANCH 2>/dev/null || git checkout -b $MAIN_BRANCH

    # 创建开发分支
    if ! git show-ref --verify --quiet refs/heads/$DEVELOP_BRANCH; then
        git checkout -b $DEVELOP_BRANCH
        if [ "$AUTO_PUSH" = "true" ]; then
            git push -u origin $DEVELOP_BRANCH
        fi
        echo -e "${GREEN}✅ 创建${DEVELOP_BRANCH}分支${NC}"
    fi

    git checkout $DEVELOP_BRANCH
    echo -e "${GREEN}✅ Git Flow初始化完成${NC}"
    echo -e "${BLUE}📋 分支结构:${NC}"
    echo "  生产分支: $MAIN_BRANCH"
    echo "  开发分支: $DEVELOP_BRANCH"
}

# 功能分支管理
manage_feature() {
    local action=$1
    local name=$2

    # 检查Git Flow是否已初始化
    check_gitflow_init

    case $action in
        "start")
            if [ -z "$name" ]; then
                echo -e "${RED}❌ 错误: 请提供功能名称${NC}"
                echo -e "${YELLOW}用法: $0 feature start <功能名称>${NC}"
                echo -e "${BLUE}示例: $0 feature start user-profile${NC}"
                exit 1
            fi

            # 检查功能分支是否已存在
            local branch_name="${FEATURE_PREFIX}${name}"
            if git show-ref --verify --quiet refs/heads/$branch_name; then
                echo -e "${RED}❌ 错误: 功能分支 '$branch_name' 已存在${NC}"
                echo -e "${YELLOW}请选择不同的功能名称或删除现有分支${NC}"
                exit 1
            fi

            echo -e "${GREEN}🌟 开始功能开发: $name${NC}"
            git checkout $DEVELOP_BRANCH
            if [ "$AUTO_PUSH" = "true" ]; then
                git pull origin $DEVELOP_BRANCH
            fi
            git checkout -b "$branch_name"
            echo -e "${GREEN}✅ 功能分支 $branch_name 已创建${NC}"
            echo -e "${BLUE}💡 提示: 开发完成后运行 '$0 feature finish $name'${NC}"
            ;;
        "finish")
            if [ -z "$name" ]; then
                echo -e "${RED}❌ 错误: 请提供功能名称${NC}"
                echo -e "${YELLOW}用法: $0 feature finish <功能名称>${NC}"
                exit 1
            fi

            # 检查功能分支是否存在
            local branch_name="${FEATURE_PREFIX}${name}"
            if ! git show-ref --verify --quiet refs/heads/$branch_name; then
                echo -e "${RED}❌ 错误: 功能分支 '$branch_name' 不存在${NC}"
                echo -e "${YELLOW}请检查功能名称是否正确${NC}"
                exit 1
            fi

            echo -e "${GREEN}🎉 完成功能开发: $name${NC}"
            git checkout $DEVELOP_BRANCH
            if [ "$AUTO_PUSH" = "true" ]; then
                git pull origin $DEVELOP_BRANCH
            fi
            git merge "$branch_name"
            if [ "$AUTO_PUSH" = "true" ]; then
                git push origin $DEVELOP_BRANCH
            fi
            if [ "$DELETE_MERGED_BRANCHES" = "true" ]; then
                git branch -d "$branch_name"
                echo -e "${GREEN}✅ 功能分支 $branch_name 已合并并删除${NC}"
            else
                echo -e "${GREEN}✅ 功能分支 $branch_name 已合并${NC}"
            fi
            ;;
        *)
            echo -e "${RED}❌ 错误: 无效的功能操作 '$action'${NC}"
            echo -e "${YELLOW}支持的操作: start, finish${NC}"
            echo -e "${BLUE}示例:${NC}"
            echo "  $0 feature start user-profile"
            echo "  $0 feature finish user-profile"
            exit 1
            ;;
    esac
}

# 发布分支管理
manage_release() {
    local action=$1
    local version=$2

    # 检查Git Flow是否已初始化
    check_gitflow_init

    case $action in
        "start")
            if [ -z "$version" ]; then
                echo -e "${RED}❌ 错误: 请提供版本号${NC}"
                echo -e "${YELLOW}用法: $0 release start <版本号>${NC}"
                echo -e "${BLUE}示例: $0 release start v1.1.0${NC}"
                exit 1
            fi

            # 验证版本号格式
            validate_version_format "$version" "release"

            # 检查发布分支是否已存在
            local branch_name="${RELEASE_PREFIX}${version}"
            if git show-ref --verify --quiet refs/heads/$branch_name; then
                echo -e "${RED}❌ 错误: 发布分支 '$branch_name' 已存在${NC}"
                exit 1
            fi

            echo -e "${GREEN}🚀 开始发布准备: $version${NC}"
            git checkout $DEVELOP_BRANCH
            if [ "$AUTO_PUSH" = "true" ]; then
                git pull origin $DEVELOP_BRANCH
            fi
            git checkout -b "$branch_name"
            echo -e "${GREEN}✅ 发布分支 $branch_name 已创建${NC}"
            echo -e "${BLUE}💡 提示: 测试完成后运行 '$0 release finish $version'${NC}"
            ;;
        "finish")
            if [ -z "$version" ]; then
                echo -e "${RED}❌ 错误: 请提供版本号${NC}"
                echo -e "${YELLOW}用法: $0 release finish <版本号>${NC}"
                exit 1
            fi

            # 检查发布分支是否存在
            local branch_name="${RELEASE_PREFIX}${version}"
            if ! git show-ref --verify --quiet refs/heads/$branch_name; then
                echo -e "${RED}❌ 错误: 发布分支 '$branch_name' 不存在${NC}"
                exit 1
            fi

            echo -e "${GREEN}🎉 完成版本发布: $version${NC}"

            # 合并到主分支
            git checkout $MAIN_BRANCH
            if [ "$AUTO_PUSH" = "true" ]; then
                git pull origin $MAIN_BRANCH
            fi
            git merge "$branch_name"
            local tag_name="${TAG_PREFIX}${version#v}"  # 移除版本号前的v，然后加上配置的前缀
            git tag -a "$tag_name" -m "Release $version"
            if [ "$AUTO_PUSH" = "true" ]; then
                git push origin $MAIN_BRANCH --tags
            fi

            # 合并到开发分支
            git checkout $DEVELOP_BRANCH
            git merge "$branch_name"
            if [ "$AUTO_PUSH" = "true" ]; then
                git push origin $DEVELOP_BRANCH
            fi

            # 删除发布分支
            if [ "$DELETE_MERGED_BRANCHES" = "true" ]; then
                git branch -d "$branch_name"
                echo -e "${GREEN}✅ 版本 $version 发布完成，分支已删除${NC}"
            else
                echo -e "${GREEN}✅ 版本 $version 发布完成${NC}"
            fi
            echo -e "${BLUE}🎯 标签 $tag_name 已创建并推送到 $MAIN_BRANCH 分支${NC}"
            ;;
        *)
            echo -e "${RED}❌ 错误: 无效的发布操作 '$action'${NC}"
            echo -e "${YELLOW}支持的操作: start, finish${NC}"
            echo -e "${BLUE}示例:${NC}"
            echo "  $0 release start v1.1.0"
            echo "  $0 release finish v1.1.0"
            exit 1
            ;;
    esac
}

# 热修复分支管理
manage_hotfix() {
    local action=$1
    local version=$2

    # 检查Git Flow是否已初始化
    check_gitflow_init

    case $action in
        "start")
            if [ -z "$version" ]; then
                echo -e "${RED}❌ 错误: 请提供版本号${NC}"
                echo -e "${YELLOW}用法: $0 hotfix start <版本号>${NC}"
                echo -e "${BLUE}示例: $0 hotfix start v1.0.1${NC}"
                exit 1
            fi

            # 验证版本号格式
            validate_version_format "$version" "hotfix"

            # 检查热修复分支是否已存在
            if git show-ref --verify --quiet refs/heads/hotfix/$version; then
                echo -e "${RED}❌ 错误: 热修复分支 'hotfix/$version' 已存在${NC}"
                exit 1
            fi

            echo -e "${GREEN}🔥 开始热修复: $version${NC}"
            git checkout $MAIN_BRANCH
            git pull origin $MAIN_BRANCH
            git checkout -b "${HOTFIX_PREFIX}$version"
            echo -e "${GREEN}✅ 热修复分支 hotfix/$version 已创建${NC}"
            echo -e "${BLUE}💡 提示: 修复完成后运行 '$0 hotfix finish $version'${NC}"
            echo -e "${RED}⚠️  注意: 这是紧急修复，请尽快完成并部署${NC}"
            ;;
        "finish")
            if [ -z "$version" ]; then
                echo -e "${RED}❌ 错误: 请提供版本号${NC}"
                echo -e "${YELLOW}用法: $0 hotfix finish <版本号>${NC}"
                exit 1
            fi

            # 检查热修复分支是否存在
            if ! git show-ref --verify --quiet refs/heads/hotfix/$version; then
                echo -e "${RED}❌ 错误: 热修复分支 'hotfix/$version' 不存在${NC}"
                exit 1
            fi

            echo -e "${GREEN}🎉 完成热修复: $version${NC}"

            # 合并到main
            git checkout $MAIN_BRANCH
            git pull origin $MAIN_BRANCH
            git merge "${HOTFIX_PREFIX}$version"
            git tag -a "$version" -m "Hotfix $version"
            git push origin $MAIN_BRANCH --tags

            # 合并到develop
            git checkout develop
            git merge "hotfix/$version"
            git push origin develop

            # 删除热修复分支
            git branch -d "hotfix/$version"
            echo -e "${GREEN}✅ 热修复 $version 完成${NC}"
            echo -e "${BLUE}🚀 请立即部署到生产环境${NC}"
            ;;
        *)
            echo -e "${RED}❌ 错误: 无效的热修复操作 '$action'${NC}"
            echo -e "${YELLOW}支持的操作: start, finish${NC}"
            echo -e "${BLUE}示例:${NC}"
            echo "  $0 hotfix start v1.0.1"
            echo "  $0 hotfix finish v1.0.1"
            exit 1
            ;;
    esac
}

# 创建标签
create_tag() {
    local version=$1
    local message=$2
    
    if [ -z "$version" ]; then
        echo -e "${RED}错误: 请提供版本号${NC}"
        exit 1
    fi
    
    if [ -z "$message" ]; then
        message="Release $version"
    fi
    
    echo -e "${GREEN}🏷️  创建标签: $version${NC}"
    git tag -a "$version" -m "$message"
    git push origin --tags
    echo -e "${GREEN}✅ 标签 $version 已创建${NC}"
}

# 查看状态
show_status() {
    echo -e "${BLUE}📊 项目状态报告${NC}"
    echo ""

    # 当前分支信息
    local current_branch=$(git branch --show-current)
    echo -e "${YELLOW}📍 当前分支:${NC} ${GREEN}$current_branch${NC}"

    # 最新标签信息
    local latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "无标签")
    echo -e "${YELLOW}🏷️  最新标签:${NC} ${GREEN}$latest_tag${NC}"

    # 分支统计
    local total_branches=$(git branch | wc -l)
    local remote_branches=$(git branch -r | wc -l)
    echo -e "${YELLOW}🌿 分支统计:${NC} 本地 $total_branches 个, 远程 $remote_branches 个"
    echo ""

    # 本地分支详情
    echo -e "${BLUE}📋 本地分支列表:${NC}"
    git branch --format="%(if)%(HEAD)%(then)* %(color:green)%(else)  %(color:reset)%(end)%(refname:short)%(color:reset) %(color:blue)(%(committerdate:relative))%(color:reset)" | head -10
    echo ""

    # 进行中的工作
    echo -e "${BLUE}🚧 进行中的工作:${NC}"

    # 检查功能分支
    local feature_branches=$(git branch | grep "^[* ]*${FEATURE_PREFIX}" | sed 's/^[* ]*//' | sed "s|^${FEATURE_PREFIX}||")
    if [ -n "$feature_branches" ]; then
        echo -e "${GREEN}  功能开发:${NC}"
        echo "$feature_branches" | while read branch; do
            if [ -n "$branch" ]; then
                local last_commit=$(git log --oneline -1 "${FEATURE_PREFIX}${branch}" 2>/dev/null | cut -d' ' -f2-)
                echo "    • $branch - $last_commit"
            fi
        done
    else
        echo -e "${GREEN}  功能开发:${NC} 无进行中的功能分支"
    fi

    # 检查发布分支
    local release_branches=$(git branch | grep "^[* ]*${RELEASE_PREFIX}" | sed 's/^[* ]*//' | sed "s|^${RELEASE_PREFIX}||")
    if [ -n "$release_branches" ]; then
        echo -e "${YELLOW}  版本发布:${NC}"
        echo "$release_branches" | while read branch; do
            if [ -n "$branch" ]; then
                local last_commit=$(git log --oneline -1 "${RELEASE_PREFIX}${branch}" 2>/dev/null | cut -d' ' -f2-)
                echo "    • $branch - $last_commit"
            fi
        done
    else
        echo -e "${YELLOW}  版本发布:${NC} 无进行中的发布分支"
    fi

    # 检查热修复分支
    local hotfix_branches=$(git branch | grep "^[* ]*${HOTFIX_PREFIX}" | sed 's/^[* ]*//' | sed "s|^${HOTFIX_PREFIX}||")
    if [ -n "$hotfix_branches" ]; then
        echo -e "${RED}  紧急修复:${NC}"
        echo "$hotfix_branches" | while read branch; do
            if [ -n "$branch" ]; then
                local last_commit=$(git log --oneline -1 "${HOTFIX_PREFIX}${branch}" 2>/dev/null | cut -d' ' -f2-)
                echo "    • $branch - $last_commit"
            fi
        done
    else
        echo -e "${RED}  紧急修复:${NC} 无进行中的热修复分支"
    fi
    echo ""

    # 版本历史
    echo -e "${BLUE}📈 版本历史 (最近5个):${NC}"
    git tag | grep "^${TAG_PREFIX}" | sort -V | tail -5 | while read tag; do
        local tag_date=$(git log -1 --format="%ci" "$tag" 2>/dev/null | cut -d' ' -f1)
        local tag_message=$(git tag -l --format="%(contents:subject)" "$tag" 2>/dev/null)
        echo "  • $tag ($tag_date) - $tag_message"
    done
    echo ""

    # 最近提交
    echo -e "${BLUE}📝 最近提交 (最近5个):${NC}"
    git log --oneline --decorate -5 | sed 's/^/  /'
    echo ""

    # 工作区状态
    echo -e "${BLUE}💼 工作区状态:${NC}"
    if git diff --quiet && git diff --cached --quiet; then
        echo -e "${GREEN}  ✅ 工作区干净${NC}"
    else
        echo -e "${YELLOW}  ⚠️  有未提交的更改${NC}"
        if ! git diff --quiet; then
            echo "    • 未暂存的更改: $(git diff --name-only | wc -l) 个文件"
        fi
        if ! git diff --cached --quiet; then
            echo "    • 已暂存的更改: $(git diff --cached --name-only | wc -l) 个文件"
        fi
    fi

    # 与远程分支的同步状态
    if git remote | grep -q origin; then
        local ahead=$(git rev-list --count HEAD..origin/$current_branch 2>/dev/null || echo "0")
        local behind=$(git rev-list --count origin/$current_branch..HEAD 2>/dev/null || echo "0")
        if [ "$ahead" -gt 0 ] || [ "$behind" -gt 0 ]; then
            echo -e "${YELLOW}  📡 与远程分支差异:${NC}"
            [ "$behind" -gt 0 ] && echo "    • 领先远程 $behind 个提交"
            [ "$ahead" -gt 0 ] && echo "    • 落后远程 $ahead 个提交"
        else
            echo -e "${GREEN}  📡 与远程分支同步${NC}"
        fi
    fi
}

# 主逻辑
# 首先检查是否在Git仓库中
check_git_repo

# 加载配置文件
load_config

case $1 in
    "init")
        init_gitflow
        ;;
    "feature")
        manage_feature $2 $3
        ;;
    "release")
        manage_release $2 $3
        ;;
    "hotfix")
        manage_hotfix $2 $3
        ;;
    "tag")
        check_gitflow_init  # 标签创建也需要初始化
        create_tag $2 "$3"
        ;;
    "status")
        show_status
        ;;
    "")
        echo -e "${YELLOW}⚠️  请提供命令参数${NC}"
        echo ""
        show_help
        ;;
    *)
        echo -e "${RED}❌ 错误: 未知命令 '$1'${NC}"
        echo ""
        show_help
        exit 1
        ;;
esac
